package com.opc.Service.impl;

import com.alibaba.fastjson.JSON;
import com.opc.Service.AsyncMessage;
import com.opc.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AsyncMessageImpl implements AsyncMessage {

    @Autowired
    private WebSocketServer webSocketServer;
    @Async
    @Override
    public void sendTest(Long waitTime,Long MAX_WAIT_TIME) {
        try {
            // 退避策略：每次重试后等待时间加倍，直到达到某个最大值
//            waitTime = Math.min(waitTime + 500, MAX_WAIT_TIME);
            Thread.sleep(waitTime);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        Map map = new HashMap<>();
        map.put("message", "..创建OPC客户端失败，重试..");
        String json = JSON.toJSONString(map);
        webSocketServer.sendToAllClient(json);
    }
}
